// components/musicList.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    listId: String,
    type: String // mostLike Or new
  },
  /**
   * 组件的初始数据
   */
  data: {
    data: null,
    type: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    update: function () {
      this.triggerEvent('update')
    },
    addToListAndPlay: function (e) {
      const index = e.currentTarget.dataset.musicid
      app.setMusicAndPlay(this.data.data.musicList[index], this.data.data.singerList[index].name);
      this.update()
    },
    addToList: function (e) {
      const index = e.currentTarget.dataset.musicid
      app.pushMusicToList(this.data.data[index].music, this.data.data[index].user.name)
      this.update()
    },
    addAllToList: function () {
      if (this.data.data != null) {
        console.log(this.data.data.length);
        for (let i = 0; i < this.data.data.length; i++) {
          app.pushMusicToList(this.data.data[i].music, this.data.data[i].user.name);
        }
        app.globalData.audioCtx.onCanplay(() => {
          app.globalData.audioCtx.play()
          app.globalData.state = "playing"
          this.update()
        })
      }
    },
  },
  lifetimes: {
    attached: function () {
      wx.showLoading({
        title: '加载中',
      })
      let that = this
      console.log(this.properties.listId);
      this.setData({
        type: this.properties.type
      })
      if (this.properties.type == 'new') {
        wx.request({
          url: `http://www.mmdccj.top:9003/music/getNewMusic`,
          method: 'GET',
          success: function (res) {
            that.setData({
              data: res.data.data
            })
            wx.hideLoading()
          }
        })
      }else{
        wx.request({
          url: `http://www.mmdccj.top:9003/music/getMostListMusic`,
          method: 'GET',
          success: function (res) {
            that.setData({
              data: res.data.data
            })
            wx.hideLoading()
          }
        })
      }

    }
  }
})